package class5;

import common.Node;
import common.TreeNode;
import common.TreeNodeUtils;

import java.util.ArrayList;
import java.util.List;

public class TestDemo {

    public static void main(String[] args) {
        TreeNode root = TreeNodeUtils.getBinarySearchTree();
        int ans = widthOfBinaryTree(root);
        System.out.println("ans = " + ans);
    }

    static int MAXN = 3001;
    static TreeNode[] queue = new TreeNode[MAXN];
    static int[] qct = new int[MAXN];
    static int l, r;

    private static int widthOfBinaryTree(TreeNode root) {
        int ans = 1;
        l = r = 0;
        queue[r] = root;
        qct[r++] = 1;
        while (l < r) {
            int size = r - l;
            ans = Math.max(ans, qct[r - 1] - qct[l] + 1);
            for (int i = 0; i < size; i++) {
                TreeNode treeNode = queue[l];
                int id = qct[l++];
                if (treeNode.left != null) {
                    queue[r] = treeNode.left;
                    qct[r++] = id * 2;
                }
                if (treeNode.right != null) {
                    queue[r] = treeNode.right;
                    qct[r++] = id * 2 + 1;
                }
            }
        }
        return ans;
    }


}
